package com.duniptech.soa.adapter;

import java.util.Iterator;

public class CoupledDevsJava extends genDevs.modeling.atomic {
	private static final long serialVersionUID = 1L;
	private genDevs.simulation.CoordinatorInterface coordinator;
	
	public CoupledDevsJava(genDevs.modeling.digraph model) {
		super(model.getName());
		
		genDevs.modeling.couprel couplings = model.getCouprel();
		Iterator<?> itr = couplings.iterator();
		while(itr.hasNext()) {
			GenCol.Pair relation = (GenCol.Pair)itr.next();
			GenCol.Pair from = (GenCol.Pair)relation.getKey();
			String fromComponentName = (String)from.getKey();
			String fromPortName = (String)from.getValue();
			GenCol.Pair to = (GenCol.Pair)relation.getValue();
			String toComponentName = (String)to.getKey();
			String toPortName = (String)to.getValue();
			if(fromComponentName.equals(model.getName()))
				this.addInport(fromPortName);
			else if(toComponentName.equals(model.getName()))
				this.addOutport(toPortName);
		}
		coordinator = new genDevs.simulation.coordinator(model);
		coordinator.initialize();
	}
	
	public void deltext(double e, genDevs.modeling.message x) {
		coordinator.simInject(e, x);
	}


	public void deltint() {
		coordinator.wrapDeltfunc(coordinator.getTN());
	}

	public genDevs.modeling.message out() {
		coordinator.computeInputOutput(coordinator.getTN());
		return (genDevs.modeling.message)coordinator.getOutput();
	}
	
	public double ta() {
		return coordinator.getTN() - coordinator.getTL();
	}

}
